import {
  $wuxSelect
} from '../../lib/index'
let remark, amount
// ////获取应用实例
// const app = getApp()
Page({

  bindPickerChange: function (e) {
    console.log(e);
    // console.log("wid:"+this.data.walletType[e.detail.value].wid);
    this.setData({
      wid: this.data.walletType[e.detail.value].wid,
      index: e.detail.value
    })
  },

  onClick(event) {
    // if(event.detail.title=="支出"){
    //   this.setData({
    //     sid=1
    //   })
    // }
    // if(event.detail.title=="收入"){
    //   this.setData({
    //     sid=2
    //   })
    // }
    let index = event.detail.index;
    this.setData({
      sid:index+1
    })
    console.log(this.data.sid);
    wx.showToast({
      title: `${event.detail.title}`,
      icon: 'none',
    });
  },


  gettitle(e) {
    console.log(e);
  },
  //请求增加
  forSubmit: function (e) {
    var approach = this.data.approach;
    var aid = this.data.aid;
    var th = this;
    var forData = e.detail.value;
    forData.approach = approach;
    forData.aid = aid;
    forData.sid = this.data.sid; //支出
    forData.wid = this.data.wid; //支出
    forData.uid = wx.getStorageSync('user').uid;
    // console.log(forData);
    if(this.data.bid > 0){
      
    forData.bid = this.data.bid;
      wx.request({
        url: 'http://localhost:8080/TheTimeSequence/BooksServlet?opr=upd',
        data: forData,
        success: function (res) {
  
          if (res.statusCode == 200) {
  
            wx.navigateTo({
              url: "../../pages/Bookkeeping/index",
  
            })
            wx.showToast({
              title: '修改成功',
            })
          } else {
            wx.showToast({
              title: '修改失败',
              duration: 10000,
  
              icon: 'none'
            })
          }
        }
      })
    }else{
      wx.request({
        url: 'http://localhost:8080/TheTimeSequence/BooksServlet?opr=add',
        data: forData,
        success: function (res) {
  
          if (res.statusCode == 200) {
  
            wx.navigateTo({
              url: "../../pages/Bookkeeping/index",
  
            })
            wx.showToast({
              title: '添加成功',
            })
          } else {
            wx.showToast({
              title: '添加失败',
              duration: 10000,
  
              icon: 'none'
            })
          }
        }
      })
    }
    this.setData({
      bid:0
    })
  },

  //备注
  remarksbin(e) {
    // console.log(e);
    remark = e.detail.value;

  },
  //金额
  moneybin(e) {
    // console.log(e);

    amount = e.detail.value;
  },
  handletap(e) {
    // console.log(e);
  },
  modthbin(e) {
    // console.log(e)
  },

  /**
   * 页面的初始数据
   */
  data: {
    text_style: "",
    user: wx.getStorageSync('user'),
    url: {},
    urlselected: {},
    sid: 1, //类型id
    approach: "", //支出类型
    payList: {}, //获取支出类型
    incomeList: {}, //获取收入类型
    wid: 1, //支付类型
    books: {}, //详情信息
    remark:"",//备注
    amount:"",//支付金额
    wid:"",//支付类型
    //支出类型是否选择
    selected: {
      1: false,
      2: false,
      3: false,
      4: false,
      5: false,
      6: false,
      7: false,
      8: false,
      9: false,
      10: false
    },
    content: '微信小程序自定义组件 https://github.com/wux-weapp/wux-weapp',
    num: "",
    walletType: ['现金', '微信', '银行卡', '其他'],
    wallet:{}
  },
  delete(e) {
    var numlength = this.data.num.length;
    var num = this.data.num.substring(0, numlength - 1);
    this.setData({
      num: num

    })
  },

  // 点击下拉显示框
  onClick1(e) {
    console.log(e.datail);
    $wuxSelect('#wux-select1').open({
      value: this.data.value1,
      options: [
        '微信',
        '支付宝',
        '银行卡',
        '花呗',
        '现金',
        '其他',
      ],
      onConfirm: (value, index, options) => {
        console.log('onConfirm', value, index, options)
        if (index !== -1) {
          this.setData({
            value1: value,
            title1: options[index],
          })
        }
      },
    })
  },
  // 输入框的input事件的执行逻辑
  handleInput(e) {
    // console.log(e.detail.value );
    this.setData({
      num: e.detail.value
    })
  },


  // 加 减 按钮的事件
  handletap(e) {
    // console.log(e);
    // 1 获取自定义属性 operation
    const operation = e.currentTarget.dataset.operation;
    this.setData({
      num: this.data.num + operation
    })
  },

  /**
   * 查询支付类型
   */
  selectWalletType: function () {
    wx.request({
      url: 'http://localhost:8080/TheTimeSequence/WalletTypeServlet?opr=walletType',
      data: {
        uid: wx.getStorageSync('user').uid
      },
      success: (e) => {
        this.setData({
          walletType: e.data
        });
        // console.log(this.data.walletType);
        // console.log(this.data.walletType[0].wtname);
      }
    })
  },
  /**
   * 查询该用户的支付钱包
   */
  selectWallet: function () {
    wx.request({
      url: 'http://localhost:8080/TheTimeSequence/WalletServlet?opr=wallet',
      data: {
        uid: wx.getStorageSync('user').uid
      },
      success: (e) => {
        this.setData({
          wallet: e.data
        });
        // console.log(this.data.wallet);
        // console.log(this.data.walletType[0].wtname);
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.selectApproaches();
    setTimeout(() => {
      this.setData({
        user: wx.getStorageSync('user'),
        content: 'Wux Weapp 是一套组件化、可复用、易扩展的微信小程序 UI 组件库。80+ 丰富的组件，能够满足移动端开发的基本需求。',
      })
      this.notice = this.selectComponent('#wux-notice-bar')

    }, 3000);
    /**
     * 点击显示详情
     */
    var that = this;
    that.setData({
      bid: options.bid
    });
    // console.log(this.data.bid);//输出本详情的信息id
    if (this.data.bid != undefined) {
      wx.request({
        url: 'http://localhost:8080/TheTimeSequence/BooksServlet?opr=books',
        data: {
          bid: this.data.bid
        },
        success: (e) => {
          this.setData({
            books: e.data.books,
            aid: e.data.books.aid,
            num:e.data.books.amount,
            sid:e.data.books.sid
          })

          this.data.selected[this.data.aid] = !this.data.selected[this.data.aid];
          var select = this.data.selected;
          this.setData({
            selected: select,
            remark:e.data.books.remark,
            wid:e.data.books.wid
          })
        }
      })
    }
    this.selectWalletType();
    this.selectWallet();
  },
  /**
   * 选中
   */
  studySelected: function (e) {

  },
  /**
   * 支出途径
   */
  selectApproaches: function (e) {
    wx.request({
      url: 'http://localhost:8080/TheTimeSequence/ApproachServlet?opr=approaches',
      success: (e) => {
        this.setData({
          payList: e.data.payList,
          incomeList:e.data.incomeList
        });
        //获取未选取的图片路径数组
        var url = {};
        for (var i = 0; i < this.data.payList.length; i++) {
          url[i] = this.data.payList[i].url;
        }
        
        this.setData({
          url: url
        })
        for (var i = 0; i < this.data.incomeList.length; i++) {
          url[i] = this.data.incomeList[i].url;
        }
        
        this.setData({
          url1: url
        })
        //获取已选取的图片路径数组
        var urlsel = {};
        for (var i = 0; i < this.data.payList.length; i++) {
          urlsel[i] = this.data.payList[i].urlselected;
        }
        this.setData({
          urlselected: urlsel
        })
        for (var i = 0; i < this.data.incomeList.length; i++) {
          urlsel[i] = this.data.incomeList[i].urlselected;
        }
        this.setData({
          urlselected1: urlsel
        })
      }
    })
  },
  approaches_selected: function (e) {
    // console.log(e);//点击获取组件信息
    // console.log("aid"+e.currentTarget.dataset.aid);
    this.setData({
      aid: e.currentTarget.dataset.aid,

      selected: {
        1: false,
        2: false,
        3: false,
        4: false,
        5: false,
        6: false,
        7: false,
        8: false,
        9: false,
        10: false
      }
    })
    // console.log(this.data.approaches);//输出approaches信息

    // console.log(this.data.selected[this.data.aid]);//当前组件是否被点击
    // var aidselected = this.data.selected[1];
    this.data.selected[this.data.aid] = !this.data.selected[this.data.aid];
    var select = this.data.selected;
    this.setData({
      selected: select
    })
    // this.setData({
    //   [aidselected]:true
    // })
    // console.log(this.data.selected);//输出所有组件是否被点击
    // for(var i=0;i<this.data.urlselected.length;i++){
    //   if(e.currentTarget.dataset.aid == i){
    //     this.data.approaches[i].
    //     this.setData({
    //     })
    //   }
    // }
  }
})